package dao.impl;

import java.io.NotSerializableException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import DBUtil.DBManager;
import DBUtil.DBUtils;
import dao.ISmsDao;
import entity.Course;
import entity.Elective;
import entity.User;
//对于数据库中的数据做增删改查
public class SmsDaoImpl extends DBUtils implements ISmsDao {	
	@Override
	public int addCourse(Elective elective) {
		String sql = "insert into elective values (?,?)";
		return commInsert(sql, elective.getUsername(),elective.getCourse_id());
	}
	// 遍历课程信息表与选课表
	@Override
	public List<Course> selectCourse(User user) throws Exception {
		String sql = "select c.* from course c join elective e on c.course_id = e.course_id where username=?";
		return commSelect(sql ,user.getUsername());
	}
    //教师数据插入选课表
	@Override
	public int insertCourse(Course course) {
		String sql = "insert into course values(?,?,?,?,?,?,?,?,?)";
		return commInsert(sql, course.getCourse_id(),course.getCourse_name(),course.getTeacher(),
		course.getPoint(),course.getTime1(),course.getTime2(),course.getLocation(),course.getLimited(),course.getSelected());
	}
    //查询课程信息表所有数据
	@Override
	public List<Course> selectAll() throws NotSerializableException, InstantiationException, IllegalAccessException {
		String sql = "select * from course ";
		return commSelect(sql );
	}
	
	// 登录验证
		public  int login(User user) {
			String sql = "select count(*) from user where username=? and password=? and role=?";
		return login(sql, user.getUsername(),user.getPassword(),user.getRole());
	}

	// 注册功能
	@Override
	public int addUser(User user) {
		String sql = "insert into user values (?,?,?,?);";//数据插入用户表
		return commInsert(sql, user.getUsername(),user.getPassword(),user.getRealname(),user.getRole());
	}

	@Override
	public List<Course> selectCourseById(Course course) throws Exception{
		String sql = "select * from course where course_id=?";//查询课程表的课程编号字段
		return commSelect(sql, course.getCourse_id());
	}

	@Override
	public int deleteCourseById(Course course) throws Exception {
		String sql ="delete from course where course_id=?";
		return commInsert(sql, course.getCourse_id());
	}
		
    //删除课程
	@Override
	public int deleteStudentCourseById(Elective elective) throws Exception {
		String sql = "delete from elective where course_id=?";
		return commInsert(sql, elective.getCourse_id());
	}
}
